<?php
jimport('joomla.application.component.model');
class svModelBangluong extends JModel
{
	//------------ tim kiem----------------------------------
	function getAllBangluong($param,$acid)
	{
		$query='';
		if($acid==0)
		{
		$clause=" FROM qlcb_bangluong as bl,qlcb_canbo as cb
							WHERE bl.bl_idcanbo = cb.cb_id ";
		$donvi=JRequest::getInt('iddonvi');
		if($donvi!=0) $clause=' FROM qlcb_bangluong as bl,qlcb_canbo as cb,qlcb_quanlychung as qlc , qlcb_phongban as pb  
								WHERE bl.bl_idcanbo = cb.cb_id AND qlc.qlc_idcanbo=cb.cb_id  
								AND qlc.qlc_idphongban=pb.pb_id AND pb.pb_iddonviquanly='.$donvi.' ';
		$phongban=JRequest::getInt('idphongban');
		if($phongban!=0) $clause=' FROM qlcb_bangluong as bl,qlcb_canbo as cb,qlcb_quanlychung as qlc   
								WHERE bl.bl_idcanbo = cb.cb_id AND qlc.qlc_idcanbo=cb.cb_id  
								 AND qlc.qlc_idphongban='.$phongban.' ';
		$canbo=JRequest::getInt('bl_idcanbo');
		if($canbo!=0) $clause=' FROM qlcb_bangluong as bl,qlcb_canbo as cb
							WHERE bl.bl_idcanbo = cb.cb_id AND cb.cb_id='.$canbo.' ';
		echo $query='SELECT bl.*,cb.cb_ten '.$clause.' 
			    ORDER BY '.$param['order'].'  '.$param['order_Dir'].'
				LIMIT '.$param['limitstart'].' , '.$param['limit'];
		}
		else
		{
			 $query='SELECT bl.* , cb.cb_ten FROM '
			.'qlcb_bangluong as bl,qlcb_canbo as cb,qlcb_useraccount as ac'
			.' WHERE bl.bl_idcanbo = cb.cb_id '
			.' AND ac.ac_idcanbo=cb.cb_id AND ac.ac_iduser='.$acid;
		}
		$result=$this->_loadObjectList($query);
		return $result;
	}
	function getBangluongByID($id)
	{
		$query='SELECT bl.*,cb.cb_ten
			    FROM qlcb_bangluong as bl,qlcb_canbo as cb
			    WHERE bl.bl_idcanbo = cb.cb_id AND bl_id='.$id;
		$result=$this->_loadObject($query);
		return $result;
	}
	function getTotal()
	{
		$clause=" FROM qlcb_bangluong as bl,qlcb_canbo as cb
							WHERE bl.bl_idcanbo = cb.cb_id ";
		$donvi=JRequest::getInt('iddonvi');
		if($donvi!=0) $clause=' FROM qlcb_bangluong as bl,qlcb_canbo as cb,qlcb_quanlychung as qlc , qlcb_phongban as pb  
								WHERE bl.bl_idcanbo = cb.cb_id AND qlc.qlc_idcanbo=cb.cb_id  
								AND qlc.qlc_idphongban=pb.pb_id AND pb.pb_iddonviquanly='.$donvi.' ';
		$phongban=JRequest::getInt('idphongban');
		if($phongban!=0) $clause=' FROM qlcb_bangluong as bl,qlcb_canbo as cb,qlcb_quanlychung as qlc   
								WHERE bl.bl_idcanbo = cb.cb_id AND qlc.qlc_idcanbo=cb.cb_id  
								 AND qlc.qlc_idphongban='.$phongban.' ';
		$canbo=JRequest::getInt('bl_idcanbo');
		if($canbo!=0) $clause=' FROM qlcb_bangluong as bl,qlcb_canbo as cb
							WHERE bl.bl_idcanbo = cb.cb_id AND cb.cb_id='.$canbo.' ';
		$query='SELECT COUNT(bl_id) '.$clause;
		$result=$this->_loadResult($query);
		return $result;
	}
	function Listcanbo()
	{
		$where="";
		$donvi=JRequest::getInt('iddonvi');
		if($donvi!=0) $where=',qlcb_quanlychung as qlc,qlcb_phongban as pb 
								 WHERE qlc.qlc_idcanbo = cb.cb_id
								 AND  qlc.qlc_idphongban = pb.pb_id 
		 						AND pb.pb_iddonviquanly='.$donvi;
		$phongban=JRequest::getInt('idphongban');
		if($phongban!=0) $where=',qlcb_quanlychung as qlc WHERE qlc.qlc_idcanbo=cb.cb_id AND qlc.qlc_idphongban='.$phongban;
		$query='SELECT DISTINCT cb.cb_id as `key`,cb.cb_ten as `text`  FROM qlcb_canbo as cb '.$where;
		return $this->_getList($query);
	}
	function ListPhongban()
	{
		$where="";
		$donvi=JRequest::getInt('iddonvi');
		if($donvi!=0) $where=' WHERE pb_iddonviquanly='.$donvi;
		$query='SELECT DISTINCT pb_id as `key`,pb_ten as `text`  FROM qlcb_phongban '.$where;
		return $this->_getList($query);
	}
	function ListDonvi()
	{
		$query='SELECT DISTINCT dvql_id as `key`,dvql_ten as `text`  FROM qlcb_donviquanly';
		return $this->_getList($query);
	}
	//----------------------------them ,sua ,xoa -------------------------------
	// xu ly su lieu
	function remove($id)
	{
		$query='DELETE FROM qlcb_bangluong WHERE bl_id='.$id;
		$this->_Execute($query);
	}
	// cac ham phuc vu truy van CSDL
	function _loadResult($query)
	{
		$db= JFactory::getDBO();
		$db->setQuery($query);
		$result=$db->loadResult();
		return $result;
	}
	function _loadObject($query)
	{
		$db= JFactory::getDBO();
		$db->setQuery($query);
		$result=$db->loadObject();
		return $result;
	}
	function _loadObjectList($query)
	{
		$db= JFactory::getDBO();
		$db->setQuery($query);
		$result=$db->loadObjectList();
		return $result;
	}
	function _Execute($query,$message)
	{
		$db= JFactory::getDBO();
		$db->setQuery($query);
		try {
			$db->query();
			return $message." thành công ";
		}
		catch (Exception $e){
			return $message." Không thành công! Vui lòng thử lại.";
		}
	}
}
?>